package cn.stylefeng.guns.modular.recy.utils;

import java.io.*;

import cn.hutool.core.bean.BeanUtil;
import com.fasterxml.jackson.databind.util.*;
import com.diboot.core.util.*;
import java.lang.reflect.*;
import com.baomidou.mybatisplus.extension.service.*;
import cn.hutool.core.bean.*;
import java.util.*;
import com.baomidou.mybatisplus.core.toolkit.*;

public class EntityBind
{
    public static <T> T bindById(final Serializable id, final Class<T> voClass) {
        final Type genericSuperclass = ClassUtil.getGenericSuperclass((Class)voClass);
        final String typeName = genericSuperclass.getTypeName();
        Class<?> aClass = null;
        try {
            aClass = Class.forName(typeName);
            final Object instance = ClassUtil.createInstance((Class)aClass, false);
            final IService serviceByEntity = ContextHelper.getServiceByEntity((Class)aClass);
            final Object entityById = serviceByEntity.getById(id);
            return bind(entityById, voClass);
        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }
    
    public static <T> T bind(final Object entity, final Class<T> voClass) {
        final List entityList = new ArrayList();
        entityList.add(entity);
        final List<T> list = MyRelationsBinder.convertAndBind((List<Object>)entityList, voClass);
        if (list.size() > 0) {
            return list.get(0);
        }
        final T instance = (T)ClassUtil.createInstance((Class)voClass, false);
        BeanUtil.copyProperties(entity, instance);
        return instance;
    }
    
    public static <T> Object bindAndGetPropertis(final Object entity, final Class<T> voClass, final String properties) {
        final String voPackage = voClass.getPackage().getName();
        final T value = bind(entity, voClass);
        final String[] property = properties.split("\\.");
        Object vo = value;
        for (int i = 0; i < property.length - 1; ++i) {
            final String p = property[i];
            final Object property2 = BeanUtil.getProperty(vo, p);
            final String className = voPackage + "." + property2.getClass().getSimpleName() + "VO";
            try {
                final Class<?> aClass = Class.forName(className);
                vo = bind(property2, aClass);
            }
            catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        if (property.length > 0) {
            final Object property3 = BeanUtil.getProperty(vo, property[property.length - 1]);
            return property3;
        }
        return null;
    }
    
    public static <T> Object bindAndGetById(final Serializable id, final Class<T> voClass, final String properties) {
        final T t = bindById(id, voClass);
        return bindAndGetPropertis(t, voClass, properties);
    }
    
    public static <T> List<T> bind(final List entityList, final Class<T> voClass) {
        return MyRelationsBinder.convertAndBind((List<Object>)entityList, voClass);
    }
    
    public static <E, VO> List<VO> convertList(final List<E> entityList, final Class<VO> voClass) {
        final List<VO> list = new ArrayList<VO>();
        for (final E e : entityList) {
            final VO instance = (VO)ClassUtil.createInstance((Class)voClass, false);
            BeanUtil.copyProperties(e, instance);
            list.add(instance);
        }
        return list;
    }
    
    public static <T> Map<String, Object> getMapById(final Serializable id, final Class<T> voClass, final String properties) {
        final T t = bindById(id, voClass);
        return bindToMap(t, voClass, properties);
    }
    
    public static <T> Map<String, Object> bindToMap(final Object entity, final Class<T> voClass, final String properties) {
        final Map<String, Object> resultMap = new HashMap<String, Object>();
        if (ObjectUtils.isEmpty(entity)) {
            return resultMap;
        }
        final String voPackage = voClass.getPackage().getName();
        final T value = bind(entity, voClass);
        final String[] property = properties.split("\\.");
        Object vo = value;
        for (int i = 0; i < property.length - 1; ++i) {
            final String p = property[i];
            final Object property2 = BeanUtil.getProperty(vo, p);
            final String className = voPackage + "." + property2.getClass().getSimpleName() + "VO";
            try {
                final Class<?> aClass = Class.forName(className);
                vo = bind(property2, aClass);
                resultMap.put(p, vo);
            }
            catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        if (property.length > 0) {
            final String expression = property[property.length - 1];
            final Object property3 = BeanUtil.getProperty(vo, expression);
            resultMap.put(expression, property3);
        }
        return resultMap;
    }
}
